// @hash v3-3F54E40AF84974A1B98A419C5F46236F5E87C3FC
// Automatically generated by ReactJS.NET. Do not edit, your changes will be overridden.
// Version: 2.2.1 (build 5682fd4) with Babel 6.3.13
// Generated at: 30/06/2016 2:15:12 PM
///////////////////////////////////////////////////////////////////////////////
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

//POLYFILLS - MOVE THIS OUT
if (!String.prototype.supplant) {
    String.prototype.supplant = function (o) {
        return this.replace(/\{([^{}]*)\}/g, function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        });
    };
}

var Calendar = {
    SelectedDate: null,
    Event: React.createClass({
        displayName: 'Event',

        render: function render() {
            var dateTimeFrom = moment(this.props.DateTimeFrom);
            var dateTimeTo = moment(this.props.DateTimeTo);

            var duration = moment.duration(dateTimeTo.diff(dateTimeFrom));
            //console.log(duration.humanize());

            var isAllDayEvent = duration.asDays() >= 1;

            return React.createElement(
                'article',
                { className: 'event event-small' },
                React.createElement(
                    'div',
                    { className: 'event__date' },
                    React.createElement(
                        'p',
                        { className: 'event__month' },
                        dateTimeFrom.format('MMM')
                    ),
                    React.createElement(
                        'p',
                        { className: 'event__day' },
                        dateTimeFrom.format('D')
                    )
                ),
                React.createElement(
                    'div',
                    { className: 'event__description' },
                    React.createElement(
                        'a',
                        { href: this.props.Url, className: 'event__description-header' },
                        this.props.Title
                    ),
                    React.createElement(
                        'p',
                        { className: 'event__description-detail' },
                        React.createElement(
                            'span',
                            { className: 'event__description-time' },
                            isAllDayEvent ? "All Day Event for " + (duration.asDays() + 1) + " days" : dateTimeFrom.format('LT') + ", " + duration.format("h [hours], m [minutes]")
                        ),
                        React.createElement('br', null),
                        this.props.Venue
                    )
                )
            );
        }
    }),
    Day: React.createClass({
        displayName: 'Day',

        onDayClicked: function onDayClicked(e) {
            e.preventDefault();
            PubSub.publish("OnCalendarDateChanged", this.props.Date);
        },
        render: function render() {
            var className = [];
            var classtd = "";
            var j = 0;

            var eventStyles = [];
            if (this.props.HasEvents) {
                jQuery.each(this.props.EventTypes, function (i, eventType) {
                    switch (eventType) {
                        case 0:
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--red' }));
                            j++;
                            className.push(React.createElement(
                                'span',
                                { key: j },
                                '  '
                            ));
                            j++;

                            break;
                        case 1:
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--green' }));
                            j++;
                            className.push(React.createElement(
                                'span',
                                { key: j },
                                ' '
                            ));
                            j++;
                            break;
                        case 2:
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--red' }));
                            j++;
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--green ' }));
                            j++;
                            break;
                        case 3:
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--secondary ' }));
                            j++;
                            className.push(React.createElement(
                                'span',
                                { key: j },
                                ' '
                            ));
                            j++;
                            break;
                        case 4:
                            className.push(React.createElement('span', { key: j, className: 'calendar__indicator calendar__indicator--orange ' }));
                            j++;
                            className.push(React.createElement(
                                'span',
                                { key: j },
                                ' '
                            ));
                            j++;
                            break;
                    }
                });
            }
            var today = moment();
            if (moment(this.props.Date).isSame(today, 'day')) {
                classtd = " today";
            }

            if (moment(this.props.Date).startOf('day').isSame(moment(Calendar.SelectedDate).startOf('day'))) {
                classtd = " active";
            }

            var date = moment(this.props.Date).format('DD/MM/YYYY');
            var url = "/News_and_Events/Events_Calendar/Events_Search_Result?expand=True&drvH=1&pnuH=1&f=" + date + "&to=" + date + "&st=0";

            //if has an event
            if (this.props.IsDayOfMonth) {
                return React.createElement(
                    'td',
                    { className: classtd },
                    React.createElement(
                        'a',
                        { href: '#', className: 'day', onClick: this.onDayClicked },
                        React.createElement(
                            'span',
                            null,
                            moment(this.props.Date).format('D')
                        )
                    ),
                    React.createElement(
                        'div',
                        { className: 'calendar__indicators' },
                        className
                    )
                );
            } else {
                return React.createElement(
                    'td',
                    { className: 'inactive' },
                    React.createElement(
                        'span',
                        { className: 'day' },
                        moment(this.props.Date).format('D')
                    )
                );
            }
        }
    }),
    Week: React.createClass({
        displayName: 'Week',

        render: function render() {
            var days = [];

            jQuery.each(this.props.Days, function (i, day) {
                days.push(React.createElement(Calendar.Day, _extends({ key: i }, day)));
            });

            return React.createElement(
                'tr',
                null,
                days
            );
        }
    }),
    Calendar: React.createClass({
        displayName: 'Calendar',

        onPreviousMonth: function onPreviousMonth() {
            PubSub.publish("AddMonth", -1);
        },
        onNextMonth: function onNextMonth() {
            PubSub.publish("AddMonth", 1);
        },
        render: function render() {
            var weeks = [];

            jQuery.each(this.props.Weeks, function (i, week) {
                weeks.push(React.createElement(Calendar.Week, _extends({ key: i }, week)));
            });

            var events = [];

            jQuery.each(this.props.Events, function (i, event) {
                if (moment(Calendar.SelectedDate).isBetween(moment(event.DateTimeFrom).add(-1, 'days'), moment(event.DateTimeTo).add(1, 'days')) && moment(event.DateTimeTo).diff(moment(Calendar.SelectedDate)) >= 0) {
                    events.push(React.createElement(Calendar.Event, _extends({ key: i }, event)));
                }
            });

            return React.createElement(
                'div',
                { className: '' },
                React.createElement(
                    'div',
                    { className: 'calendar__month text-center' },
                    React.createElement(
                        'a',
                        { href: '#', className: 'calendar__month__prev', onClick: this.onPreviousMonth },
                        React.createElement('i', { className: 'fa fa-2x fa-angle-left' })
                    ),
                    React.createElement(
                        'h4',
                        null,
                        moment(this.props.SelectedDate).format('MMMM YYYY')
                    ),
                    React.createElement(
                        'a',
                        { href: '#', className: 'calendar__month__next', onClick: this.onNextMonth },
                        React.createElement('i', { className: 'fa fa-2x fa-angle-right' })
                    )
                ),
                React.createElement(
                    'table',
                    { className: 'calendar__table' },
                    React.createElement(
                        'thead',
                        null,
                        React.createElement(
                            'tr',
                            null,
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Mon'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Tue'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Wed'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Thu'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Fri'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Sat'
                            ),
                            React.createElement(
                                'th',
                                { width: '14%' },
                                'Sun'
                            )
                        )
                    ),
                    React.createElement(
                        'tbody',
                        null,
                        weeks
                    )
                ),
                React.createElement(
                    'div',
                    { className: 'border-top' },
                    React.createElement(
                        'span',
                        null,
                        React.createElement(
                            'strong',
                            null,
                            'Legends'
                        )
                    ),
                    React.createElement(
                        'ul',
                        { className: 'inline-list text-small' },
                        React.createElement(
                            'li',
                            null,
                            React.createElement('span', { className: 'calendar__indicator calendar__indicator--large calendar__indicator--green' }),
                            ' House'
                        ),
                        React.createElement(
                            'li',
                            null,
                            React.createElement('span', { className: 'calendar__indicator calendar__indicator--large calendar__indicator--red' }),
                            ' Senate'
                        ),
                        React.createElement(
                            'li',
                            null,
                            React.createElement('span', { className: 'calendar__indicator calendar__indicator--large calendar__indicator--orange' }),
                            ' Event'
                        ),
                        React.createElement(
                            'li',
                            null,
                            React.createElement('span', { className: 'calendar__indicator calendar__indicator--large calendar__indicator--secondary' }),
                            ' Public holiday'
                        )
                    )
                ),
                events.length > 0 ? events : React.createElement(
                    'article',
                    { className: 'event event--small' },
                    React.createElement(Alerts.Info, { message: 'No events to show for the selected date.' })
                ),
                React.createElement(
                    'ul',
                    { className: 'no-bullet border-top text-small' },
                    React.createElement(
                        'li',
                        null,
                        React.createElement(
                            'a',
                            { href: '../News_and_Events/Events_Calendar' },
                            'Full calendar of events'
                        )
                    ),
                    React.createElement(
                        'li',
                        null,
                        React.createElement(
                            'a',
                            { href: '../About_Parliament/Sitting_Calendar' },
                            'Sitting calendar'
                        )
                    )
                )
            );
        }
    }),
    Control: React.createClass({
        displayName: 'Control',

        getInitialState: function getInitialState() {
            return {
                calendar: null,
                isLoading: true,
                selectedDate: null
            };
        },
        eventRefresh: function eventRefresh() {

            //var dateQuery1 = this.state.selectedDate ? "/api/calendarEvents?startDateTime=" + moment(this.state.selectedDate).format("YYYY-M-D") : "/api/calendarEvents";
            //var testweek = this.state.calendar.Weeks;
            //$.get(dateQuery1, (response) => {
            //    if (true) {
            //        this.state.selectedDate = response.SelectedDate;
            //        //this.props.Events : response.Events;
            //        Calendar.SelectedDate = response.SelectedDate;
            //        //response.Weeks = this.state.Weeks;
            //        response.Weeks = testweek;
            //        this.setState({ calendar: response, isLoading: false });
            //    }
            //});

        },
        refresh: function refresh() {
            var _this = this;

            var dateQuery = this.state.selectedDate ? "/api/calendar?date=" + moment(this.state.selectedDate).format("YYYY-M-D") : "/api/calendar";

            $.get(dateQuery, function (response) {
                if (_this.isMounted()) {
                    _this.state.selectedDate = response.SelectedDate;
                    Calendar.SelectedDate = response.SelectedDate;
                    _this.setState({ calendar: response, isLoading: false });
                }
            });
        },
        componentDidMount: function componentDidMount() {
            this.refresh();

            this.PubSub_Topic_TotalPages = PubSub.subscribe("OnCalendarDateChanged", (function (topic, selectedDate) {
                this.state.selectedDate = selectedDate;
                Calendar.SelectedDate = selectedDate;
                this.setState({ selectedDate: Calendar.SelectedDate });
                //this.eventRefresh();                   
            }).bind(this));

            this.PubSub_Topic_TotalPages = PubSub.subscribe("AddMonth", (function (topic, count) {
                //this.state.selectedDate = moment(this.state.selectedDate).add(count, 'months').toDate();
                var date = moment(this.state.selectedDate).add(count, 'months').toDate();
                this.state.selectedDate = new Date(date.getFullYear(), date.getMonth(), 1);

                this.setState({ isLoading: true });
                this.refresh();
            }).bind(this));
        },
        render: function render() {
            return React.createElement(
                'div',
                { className: 'calendar--sidebar' },
                React.createElement(
                    'a',
                    { className: 'js-canvas__close', href: '#', 'aria-label': 'Close' },
                    '×'
                ),
                React.createElement(
                    'h3',
                    null,
                    'Parliament Calendar'
                ),
                this.state.isLoading ? React.createElement(Alerts.Loader, { message: '' }) : React.createElement(Calendar.Calendar, this.state.calendar)
            );
        }
    }),
    Init: function Init() {
        ReactDOM.render(React.createElement(Calendar.Control, null), document.getElementById("parliamentary-calendar-wrapper"));
    }
};